package club.hicode.daydayup.reflect;

import org.junit.Test;
import org.springframework.objenesis.Objenesis;
import org.springframework.objenesis.ObjenesisStd;

import java.util.HashSet;
import java.util.Set;

/**
 * Ref 的测试
 *
 * @author Liu Chunfu
 * @date 2018-05-28 上午9:34
 **/
public class RefTest {

    @Test
    public void testJdbc() throws ClassNotFoundException {
        Class<?> aClass = Class.forName("club.hicode.daydayup.reflect.RefPerson");
        Class<?> aClass2 = Class.forName("club.hicode.daydayup.reflect.RefPerson");
        Class<?> aClass3 = Class.forName("club.hicode.daydayup.reflect.RefPerson");
    }

    @Test
    public void refTest0() throws ClassNotFoundException {
        Class<?> clazz = Class.forName("com.mysql.cj.jdbc.Driver");
        System.out.println(clazz);
        Object object = new Object();

    }

    @Test
    public void refTest1() throws Exception {
        Class<?> clazz = Class.forName("club.hicode.daydayup.reflect.RefPerson");
        //RefPerson refPerson = (RefPerson) targetInterface.newInstance();
        // newInstance()是实现IOC、反射、面对接口编程和依赖倒置等技术方法的必然选择，new只能实现具体类的实例化，不适合于接口编程。
        // newInstance: 弱类型。低效率。只能调用无参构造。
        RefPerson refPerson = (RefPerson) clazz.newInstance();
        refPerson.setAge(11);
        refPerson.setName("LCF");

        System.out.println(refPerson);

    }

    @Test
    public void refTest3() throws ClassNotFoundException, IllegalAccessException, InstantiationException {
        Class<?> clazz = Class.forName("club.hicode.daydayup.reflect.RefPerson");
        RefPerson refPerson = (RefPerson) clazz.newInstance();
    }

    @Test
    public void refTest4() throws ClassNotFoundException {
        Class<?> clazz = Class.forName("club.hicode.daydayup.reflect.RefPerson");
        org.objenesis.Objenesis objenesis = new org.objenesis.ObjenesisStd(true);
        RefPerson refPerson = (RefPerson) objenesis.newInstance(clazz);
        System.out.println(refPerson);
    }

    @Test
    public void refTest5() throws ClassNotFoundException {
        Class<?> clazz = Class.forName("club.hicode.daydayup.reflect.RefPerson");
        org.springframework.objenesis.Objenesis objenesis = new org.springframework.objenesis.ObjenesisStd(true);
        RefPerson refPerson = (RefPerson) objenesis.newInstance(clazz);
        System.out.println(refPerson);
    }
}
